<template>
	<view class="finish-order">

		<view class="address">
			<!-- 默认 -->
			<view class="default-address" @click="changAddress()">
				<view class="left">
					<text class="iconfont">&#xe64b;</text>
				</view>
				<view class="right">
					<view class="list-1" v-if="is_exist">
						<text class="name">收货人： {{address.name}}</text>
						<text class="num">{{address.phone}}</text>
					</view>
					<view class="list-2" v-if="is_exist">
						{{address.province + address.city + address.district + address.address}}
					</view>
					<view class="list-n" v-else>
						您还没有添加地址，点击这里添加~~
					</view>
					<view class="list-3" v-if="address.is_default == 0">
						默认
					</view>
					<view class="list-4">
						<text class="iconfont">&#xe708;</text>
					</view>
				</view>
			</view>
		</view>

		<!-- 订单信息部分 -->
		<!-- v-for="(item,index) in datas" :key="index" -->
		<view class="fo-info1">
			<view class="fo-title">
				<text>{{datas.shop_name}}</text>
				<text class="iconfont">&#xe600;</text>
			</view>
			
			<!-- 每条商品 start -->
			<view class="fo-content">
				<image :src="datas.product_img" mode="aspectFill" class="img"></image>
				<view class="fo-r">
					<view class="r-1">
						{{datas.product_name}}
					</view>
					<view class="r-2">
						<view class="left">
							￥{{datas.product_nowprice * 1}}
						</view>
						<view class="right">
							
							<tui-numberbox :value="datas.product_quantity" :max="datas.product_pronums" @change="change($event,datas)" class="number-box"></tui-numberbox>
						</view>
					</view>
				</view>
			</view>
			<!-- 每条商品 end -->
			
			<view class="yh" v-if="datas.product_addgift == 1">
				<!-- od.or_discount -->
				<!-- <text>{{lastlyPrice.fullPrice}}</text> -->
				已享受满减，优惠<text>{{ lastlyPrice.cutsPrice }}</text>元
			</view>

			<view class="ps">
				<view class="l">
					配送方式
				</view>
				<view class="r" @click="onExpress(datas)">
					{{datas.product_delivery_name}}<text class="iconfont">&#xe600;</text>
				</view>
			</view>

			<view class="order-notes">
				<view class="l">
					订单备注
				</view>
				
				<input type="text" v-model.trim="datas.order_comment" maxlength="20" placeholder="请先和商家协商一致" placeholder-style="color: #999;" />
			</view>

			<view class="footer">
				<text class="ft-l">共{{ datas.product_quantity }}件商品</text>
				<text class="ft-m">合计：</text>
				<text class="ft-r">￥{{  lastlyPrice.discountPrice }}</text>
			</view>
		</view>


		<view class="fo-bottom">
			<view class="bt-2">
				<text class="piece">共{{ datas.product_quantity }}件</text>
				<text class="xiaoji1">合计:</text>
				<text class="price">￥{{ lastlyPrice.discountPrice }}</text>
			</view>
			<button type="primary" @click="settlement">结算</button>
		</view>




	</view>
</template>

<script>
	import tuiNumberbox from "@/components/numberbox/numberbox";
	import { mapGetters,mapMutations,mapState } from "vuex";
	export default {
		components: {
			tuiNumberbox
		},
		data() {
			return {
				pro_id: '',
				quantity: 1, // 购买数量
				reduce1: '' , // 满减金额
				address: {},
				address1: {
					name: '***',
					phone: '*********',
					is_default: -1,
				},
				is_exist: true, // 判断是否有地址
				datas: {}, //
				datas10: {},
				orderId: '',
				datas1: [{
						shopname: '瑞派宠物店',
						img: '/static/imgs/image2.png',
						goodsname: '皇家猫粮室内专用10kg，满50送储粮桶',
						price: 9.9,
						num: 1,
						amount: '',
						express: '送货上门',
						expressId: '0',
						comment: '',
						limit: 5,
						benefitid: [{
								benefitid: 1,
								amout_srtart: "30.0000",
								amout_end: "0.0000",
								discount: "5.0000"
							},
							{
								benefitid: 2,
								amout_srtart: "40.0000",
								amout_end: "0.0000",
								discount: "10.0000"
							},
							{
								benefitid: 4,
								amout_srtart: "50.0000",
								amout_end: "0.0000",
								discount: "20.0000"
							},
							{
								benefitid: 6,
								amout_srtart: "100.0000",
								amout_end: "0.0000",
								discount: "50.0000"
							}
						]
					},
					{
						shopname: '瑞派宠物店',
						img: '/static/imgs/image2.png',
						goodsname: '皇家猫粮室内专用10kg，满50送储粮桶',
						price: 19.9,

						num: 2,
						amount: '',
						express: '送货上门',
						expressId: '0',
						comment: '',
						limit: 10,
						benefitid: [{
								benefitid: 1,
								amout_srtart: "30.0000",
								amout_end: "0.0000",
								discount: "5.0000"
							},
							{
								benefitid: 2,
								amout_srtart: "40.0000",
								amout_end: "0.0000",
								discount: "10.0000"
							},
							{
								benefitid: 4,
								amout_srtart: "50.0000",
								amout_end: "0.0000",
								discount: "20.0000"
							},
							{
								benefitid: 6,
								amout_srtart: "100.0000",
								amout_end: "0.0000",
								discount: "50.0000"
							}
						]
					}

				]
			}
			
		},
		onLoad(option) {
			
			this.pro_id = option.pro_id;
			this.quantity = option.quantity;
			this.getOrder(option.pro_id,option.quantity);
			
			
		},
		onShow() {
			let token = uni.getStorageSync('lph-token') || '';
			if(token) {
				this.judgeAddress();
			}
		},
		computed: {
			...mapState(['orderData']),
			lastlyPrice() { // 最后的价格
				let data = this.datas;
				let price = ((Number(data.product_quantity) * 100) * (parseFloat(data.product_nowprice) * 100)) / 10000;
				let discountPrice = price; // 满减后的价格
				let cutsPrice = 0; // 减价格
				let fullPrice = 0; // 满  价格
				let benefit = data.benefit || [];
				if(data.product_addgift == 1) {
					benefit.some((item) => {
						if(parseFloat(item.addgift_amout_srtart) <= price ){
							discountPrice = price - parseFloat(item.addgift_discount);
							cutsPrice = parseFloat(item.addgift_discount);
							fullPrice = parseFloat(item.addgift_amout_srtart);
						}else {
							discountPrice = price;
							cutsPrice = 0;
							fullPrice = 0;
						}
					})
				}
				
				return {discountPrice,cutsPrice,fullPrice};
			},
			
			allNum() {
				// let a = 0;
				// this.datas.forEach((val) => {
				// 	a += val.num;
				// })
				return this.datas.num;
			},
			allPrice() {
				let a = 0;
				// this.datas.forEach((val) => {
				// 	a += (+val.amount);
				// })
				a += (+this.datas.amount);
				return a.toFixed(2);
			},
			
		},
		
		filters: {
			float: (val) => { // 浮点数保留2位，返回值为字符串
				return val.toFixed(2);
			},
			reduction: (val) => { // 处理满减后的金额
				var countPrice = 0;
				let a = val.benefit.map((item) => {
					let price = val.price * val.num;
					if (item.amout_srtart <= price) {
						countPrice = price - item.discount;
						return countPrice;
					} else {
						countPrice = price;
						return countPrice
					}
				})
				val.amount = Math.min.apply(null, a).toFixed(2);
				return Math.min.apply(null, a).toFixed(2)
			},
			reduce: (val) => { // 计算优惠价
				let a = (val.price * val.num - val.amount).toFixed(2);
				return a;
			}
		},
		
		methods: {
			...mapMutations(['setOrderData']),
			async judgeAddress() { // 地址的判断
				// 1. 后台传过来地址
				// 2. 点击更换，使用缓存中的地址
				// 3. 两个地址都有的情况下，优先使用哪一个？
				let addressStore = uni.getStorageSync('lph-address');
				if ('addressid' in addressStore) {
					this.is_exist = true;
					this.address = addressStore;
					return
				}
				let a = await this.$http.getShipAddress();
				if (a.code == 404) { //  没有添加过地址的清空判断
					this.is_exist = false;
					return;
				}
				this.is_exist = true;
				this.address = a.data[0];
			},
			
			async getOrder(id,num) { // 获取订单数据
				let a = await this.$http.confirmOrder({pro_id: id, quantity: num});
				this.datas = a.data;
				
			},
			
			async settlement() { // 结算
				let jsData = {
					action: 'Immediate',
					shop_id: this.datas.shop_id,
					product_id: this.datas.product_id,
					price: this.lastlyPrice.discountPrice,
					quantity: this.datas.product_quantity,
					discount: this.lastlyPrice.cutsPrice,
					distype: this.datas.product_distype, // 分类ID
					delivery_type: this.datas.product_delivery,
					user_address_id: this.address.addressid,
					order_comment: this.datas.order_comment
				}
				let zzz = JSON.stringify(jsData);
				
				uni.navigateTo({
					url: `/pages/discountMall/pay?content=${zzz}`
				})
			},
			change(e, od) { // 数字选择
				this.$set(od, 'product_quantity', e.value);
			},
			changAddress() { // 更换收货地址
				uni.navigateTo({
					url: '/pages/set/shipAddress?position=1'
				})
			},
			onExpress(item) { // 选择送货方式
				let list = ['自提', '送货上门'];
				uni.showActionSheet({
					itemList: list,
					success: (res) => {
						this.$set(item, 'product_delivery_name', list[res.tapIndex])
						let id = res.tapIndex + 1;
						this.$set(item, 'product_delivery', id)
					}
				})
			},
		},
	}
</script>

<style lang="scss" scoped>
	@import 'static/css/smallCommon.scss';
</style>
